package cn.gdcp.sqlitetest;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

    Button upGradeDataBtn;
    Button addDataBtn;
    Button deleteDataBtn;
    Button queryDataBtn;
    MyDatabaseHelper myDatabaseHelper;
    SQLiteDatabase database;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initDatabase();
        initButtonView();
        initOnclickButton();
    }
    private void initDatabase() {
        myDatabaseHelper=new MyDatabaseHelper(this,"BookStore",null,2);
         database=myDatabaseHelper.getReadableDatabase();
    }

    private void initOnclickButton() {
        addDataBtn.setOnClickListener(this);
        upGradeDataBtn.setOnClickListener(this);
        deleteDataBtn.setOnClickListener(this);
        queryDataBtn.setOnClickListener(this);
    }

    private void initButtonView() {

        addDataBtn=findViewById(R.id.add_data);
        upGradeDataBtn=findViewById(R.id.upgrade_data);
        deleteDataBtn=findViewById(R.id.delete_data);
        queryDataBtn=findViewById(R.id.query_data);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_data:
                addData();
                break;
            case R.id.upgrade_data:
                upGradeData();
                break;
            case R.id.delete_data:
                deleteData();
                break;
            case R.id.query_data:
                queryData();
                break;
        }
    }
    private void queryData() {
      Cursor cursor=database.rawQuery("select * from Book",null);
        if (cursor.moveToFirst()){
            do {
                String name=cursor.getString(cursor.getColumnIndex("name"));
                String id=String.valueOf(cursor.getInt(cursor.getColumnIndex("id")));
                String height=String.valueOf(cursor.getInt(cursor.getColumnIndex("height")));
                Log.e("name",name);
                Log.e("id",id);
                Log.e("height",height);
            }while (cursor.moveToNext());


        }
        cursor.close();
    }
    private void deleteData() {
    }

    private void upGradeData() {
    }

    private void addData() {
    }

}
